/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpt.bd.cenario;

import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import rpt.lib.CCalendarioComboBox;

/**
 *
 * @author Roberto
 */
public class CEstruturaRegistro {

    CGlobal fGlobal;
    String fNomeChavePrimaria = null;
    String fNomeCampoDescricao = null;
    Object fCondicao = null;
    ArrayList<CEstruturaCampoRegistroBd> fCamposEstruturaRegistroBd = null;

    FocusAdapter fAdaptadorFoco = null;
    KeyAdapter fAdaptadorTeclado = null;
    Component fComponenteFocado = null;
    int fIndiceCampoFocado = -1;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public ArrayList<CEstruturaCampoRegistroBd> getCamposEstruturaRegistroBd() {
        return fCamposEstruturaRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaRegistro() {
        super();
        fCamposEstruturaRegistroBd = new ArrayList();
/*
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
 */
        fAdaptadorFoco = new FocusAdapter() {

            /**************************************************************************/
            /***                                                                    ***/
            /**************************************************************************/

            @Override
            public void focusGained(FocusEvent evt) {
                eventoGanhaFoco(evt);
            }

            /**************************************************************************/
            /***                                                                    ***/
            /**************************************************************************/

            @Override
            public void focusLost(FocusEvent evt) {
                eventoPerdeFoco(evt);
            }
        };

        /**************************************************************************/
        /***                                                                    ***/
        /**************************************************************************/

        fAdaptadorTeclado = new KeyAdapter() {

        /**************************************************************************/
        /***                                                                    ***/
        /**************************************************************************/

        @Override
        public void keyPressed(java.awt.event.KeyEvent evt) {
            soltouTecla(evt);
        }
//          public void keyReleased(java.awt.event.KeyEvent evt) {
//              soltouTecla(evt);
//          }
    };
}

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    protected void eventoGanhaFoco(FocusEvent evt) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    protected void eventoPerdeFoco(FocusEvent evt) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    protected void soltouTecla(KeyEvent evt) {
        eventoSoltouTecla(evt);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoSoltouTecla(KeyEvent evt) {
        final char vCaracter = evt.getKeyChar();
        final int vCodigo = evt.getKeyCode();
        boolean componenteEspecial = false;
        if (vCodigo == KeyEvent.VK_ENTER) {
            componenteEspecial = (fComponenteFocado instanceof JTextArea);
            //EStatusOperacao vStatusOperacao = fPertinenciaBd.getStatusOperacao();
            int vAlt = (evt.getModifiers() & KeyEvent.ALT_MASK);
            if ((vAlt > 0) || (!componenteEspecial)) {
                focarProximoComponente();
            }
        }
        if (vCodigo == KeyEvent.VK_DOWN) {
            componenteEspecial =
                    (fComponenteFocado instanceof JTextArea) ||
                    (fComponenteFocado instanceof JComboBox);
            //EStatusOperacao vStatusOperacao = fPertinenciaBd.getStatusOperacao();
            int vAlt = (evt.getModifiers() & KeyEvent.ALT_MASK);
            if ((vAlt > 0) || (!componenteEspecial)) {
                focarProximoComponente();
            }
        }
        if (vCodigo == KeyEvent.VK_UP) {
            //EStatusOperacao vStatusOperacao = fPertinenciaBd.getStatusOperacao();
            int vAlt = (evt.getModifiers() & KeyEvent.ALT_MASK);
            componenteEspecial =
                    (fComponenteFocado instanceof JTextArea) ||
                    (fComponenteFocado instanceof JComboBox);
            if ((vAlt > 0) || (!componenteEspecial)) {
                focarComponenteAnterior();
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public boolean focarComponenteAnterior() {
        return false;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public boolean focarProximoComponente() {
        return false;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CGlobal getGlobal() {
        return fGlobal;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void adicionarEstruturaCampoRegistroBd(CEstruturaCampoRegistroBd aEstruturaCampoRegistroBd) {
        fCamposEstruturaRegistroBd.add(aEstruturaCampoRegistroBd);
        Component vComponente = aEstruturaCampoRegistroBd.getComponente();
        if (vComponente != null) {
            if (vComponente instanceof CCalendarioComboBox)
                ((CCalendarioComboBox)vComponente).getComponenteTextoFormatado().addKeyListener(fAdaptadorTeclado);
            else
                vComponente.addKeyListener(fAdaptadorTeclado);
            if (vComponente instanceof CCalendarioComboBox)
                ((CCalendarioComboBox)vComponente).getComponenteTextoFormatado().addFocusListener(fAdaptadorFoco);
            else {
                vComponente.addFocusListener(fAdaptadorFoco);
                vComponente.addPropertyChangeListener(new PropertyChangeListener() {
                    public void propertyChange(PropertyChangeEvent evt) {
                        componentPropertyChange(evt);
                    }
                });
            }
        }
    }
    
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void componentPropertyChange(java.beans.PropertyChangeEvent evt) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void limparComponentes() {
        for (CEstruturaCampoRegistroBd ec : fCamposEstruturaRegistroBd) {
            ec.setValorComponente(null);
        };
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaCampoRegistroBd getEstruturaCampoRegistroBd(int aIndice) {
        return (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(aIndice);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
/*
    public CEstruturaCampoRegistroBd getEstruturaCampoRegistroBd(String aNomeCampo) {
        return (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(aIndice);
    }
/*
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getNumCamposRegistroBd(int aTipoCampo) {
        int vNumCamposCabecalhoRegistroBd = 0;
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        int vNumCampos = fCamposEstruturaRegistroBd.size();
        for (int vContador = 0; vContador < vNumCampos; vContador++) {
            vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            switch(aTipoCampo) {
                case 0: {
                    vNumCamposCabecalhoRegistroBd++;
                    break;
                }
                case 1: {
                    if (vEstruturaCampoRegistroBd.getCampoGradeBd())
                        vNumCamposCabecalhoRegistroBd++;
                    break;
                }
                case 2: {
                    if (!vEstruturaCampoRegistroBd.getCampoGradeBd())
                        vNumCamposCabecalhoRegistroBd++;
                    break;
                }
            }
        }
        return vNumCamposCabecalhoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getNumCamposRegistroBd() {
        return getNumCamposRegistroBd(CConstanteBd.TODOS_CAMPOS);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getNumCamposCabecalhoRegistroBd() {
        return getNumCamposRegistroBd(CConstanteBd.CAMPOS_CABECALHO);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getNumCamposComplementoRegistroBd() {
        return getNumCamposRegistroBd(CConstanteBd.CAMPOS_COMPLEMENTARES);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getNomeChavePrimaria() {
        return fNomeChavePrimaria;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getNomeCampoDescricao() {
        return fNomeCampoDescricao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public Object getCondicao() {
        return fCondicao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getIndiceEstruturaCampoRegistroBd(CEstruturaCampoRegistroBd aEstruturaCampoRegistroBd) {
        int vIndice = -1;
        int vNumCampos = fCamposEstruturaRegistroBd.size();
        for (int vContador = 0;((vContador < vNumCampos) && (vIndice == -1)); vContador++) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            if (vEstruturaCampoRegistroBd == aEstruturaCampoRegistroBd)
                vIndice = vContador;
        }
        return vIndice;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
/*
    public void setValorCampoBdChavePrimaria(int aValorChavePrimaria) {
        CEstruturaCampoRegistroBd e = getEstruturaCampoRegistroBd(fNomeChavePrimaria);
        //.setValorCampoBdChavePrimaria(aValorChavePrimaria);
    }
*/
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaCampoRegistroBd getEstruturaCampoRegistroBdComponente(Component aComponente) {
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBdLocalizada = null;
        int vNumCampos = fCamposEstruturaRegistroBd.size();
        for (int vContador = 0;((vContador < vNumCampos) && (vEstruturaCampoRegistroBdLocalizada == null)); vContador++) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            if (vEstruturaCampoRegistroBd.getComponente() == aComponente)
                vEstruturaCampoRegistroBdLocalizada = vEstruturaCampoRegistroBd;
        }
        return vEstruturaCampoRegistroBdLocalizada;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public boolean focarPrimeiroComponente() {
        boolean vEncontrou = false;
        int vNumCampos = fCamposEstruturaRegistroBd.size();
        for (int vContador = 0;((vContador < vNumCampos) && (!vEncontrou)); vContador++) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            if (vEstruturaCampoRegistroBd.getCampoEditavel()) {
                fIndiceCampoFocado = vContador;
                vEstruturaCampoRegistroBd.setFoco();
                vEncontrou = true;
            }
        }
        return vEncontrou;
    }

    public void atribuirTamanhosComponentes(ResultSet aRs) {
        try {
            aRs.first();
        } catch (SQLException ex) {
            Logger.getLogger(CEstruturaRegistroBdCenarioEdicao.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
        ResultSetMetaData rsmd = null;
        int vNumCamposRs = 0;
        try {
            rsmd = aRs.getMetaData();
            vNumCamposRs = rsmd.getColumnCount();
        } catch (SQLException ex) {
            Logger.getLogger(CEstruturaRegistroBdCenarioEdicao.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
        int vNumCamposEstrut = fCamposEstruturaRegistroBd.size();
        for (int vContador = 0; vContador < vNumCamposEstrut; vContador++) {
            CEstruturaCampoRegistroBd ecrbd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            String vNomeCampoBd1 = ecrbd.getNomeBd().trim().toUpperCase();
            boolean vAchou = false;
            boolean vContinuar = true;
            boolean vFalhou = false;
            for (int vContador2 = 0; ((vContinuar) && (vContador2 < vNumCamposRs)); vContador2++) {
                String vNomeCampoBd2;
                try {
                    String s = rsmd.getColumnName(vContador2+1);
                    vNomeCampoBd2 = s.trim().toUpperCase();
                    if (vNomeCampoBd2.equals(vNomeCampoBd1)) {
                        vAchou = true;
                        vContinuar = false;
                        int vTam = rsmd.getColumnDisplaySize(vContador2+1);
                        Component c = ecrbd.getComponente();
                        if (c instanceof JTextField) {
                            JTextField tf = (JTextField)c;
                            tf.setColumns(vTam);
                        }
                    }
                } catch (SQLException ex) {
                    vContinuar = false;
                    vFalhou = true;
                    Logger.getLogger(CEstruturaRegistroBdCenarioEdicao.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoAntesInserirRegistro() {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoDepoisInserirRegistro(CRegistro aRegistro) {
    }


    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoAntesGravarRegistro() {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoDepoisGravarRegistro(CRegistro aRegistro) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoAntesMoverRegistro() {
        int n = this.getNumCamposRegistroBd();
        for (int i = 0; i < n; i++) {
            getEstruturaCampoRegistroBd(i).eventoAntesMoverRegistro();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoDepoisMoverRegistro(CRegistro aRegistro) {
        int n = this.getNumCamposRegistroBd();
        for (int i = 0; i < n; i++) {
            getEstruturaCampoRegistroBd(i).eventoDepoisMoverRegistro(aRegistro);
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoAntesAtualizarPainelBd(CRegistro aRegistro) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoDepoisAtualizarPainelBd(CRegistro aRegistro) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
}
